<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="shopping.css">
</head>

<body>
    <div id="app">
        <div class="shoppingBar">
            <h2>购物清单</h2>
            <table>
                <tbody>
                    <tr>
                        <th><input type="checkbox" title="全选"></th>
                        <th>商品名称</th>
                        <th>数量</th>
                        <th>单价（元）</th>
                        <th>金额（元）</th>
                        <th>操作</th>
                    </tr>
                    <tr class="commodity">
                        <th><input type="checkbox" class="input" value="1"></th>
                        <th class="content"><img src="images/1.jpg" width="100px" height="100px">
                            <p>电视机 </p>
                        </th>
                        <th><button type="button" class="reduce">
                                -
                            </button> <span class="quantity">
                                1
                            </span> <button type="button" class="add">
                                +
                            </button></th>
                        <th>
                            200
                        </th>
                        <th class="money">
                            200
                        </th>
                        <th><button class="glyphicon glyphicon-remove">删除
                            </button></th>
                    </tr>
                    <tr class="commodity">
                        <th><input type="checkbox" class="input" value="2"></th>
                        <th class="content"><img src="images/2.jpg" width="100px" height="100px">
                            <p>洗衣机 </p>
                        </th>
                        <th><button type="button" class="reduce">
                                -
                            </button> <span class="quantity">
                                1
                            </span> <button type="button" class="add">
                                +
                            </button></th>
                        <th>
                            150
                        </th>
                        <th class="money">
                            150
                        </th>
                        <th><button class="glyphicon glyphicon-remove">删除
                            </button></th>
                    </tr>
                    <tr class="commodity">
                        <th><input type="checkbox" class="input" value="3"></th>
                        <th class="content"><img src="images/3.jpg" width="100px" height="100px">
                            <p>冰箱 </p>
                        </th>
                        <th><button type="button" class="reduce">
                                -
                            </button> <span class="quantity">
                                1
                            </span> <button type="button" class="add">
                                +
                            </button></th>
                        <th>
                            100
                        </th>
                        <th class="money">
                            100
                        </th>
                        <th><button class="glyphicon glyphicon-remove">删除
                            </button></th>
                    </tr>
                </tbody>
            </table>
            <div class="shoppingBar-footer">
                <div class="manage"><span class="delAll">
                        删除所选商品
                    </span> <span class="return">
                        继续购物
                    </span></div> <button id="go" type="button">
                    去结算
                </button>
                <div class="buy"><span>
                        0
                    </span>
                    件商品总计:
                    <span class="allMoney">
                        ￥0
                    </span>
                </div>
            </div>
        </div>
    </div>
    <script>
        let checkDom = document.querySelectorAll(".input");
        let checkAll = document.querySelector("th>input");
        //全选
        let autoCheck = function () {
            let that = this;
            checkDom.forEach(function (item) {
                item.checked = that.checked;
            })
            getTotal();
        }
        checkAll.addEventListener('click', autoCheck);
        //反全选
        checkDom.forEach(function (item) {
            item.onclick = function () {
                checkAll.checked = checkDom.length == document.querySelectorAll(".input:checked").length;
            }
        })
        //添加、减少数量//金额
        let moneyDom = document.querySelectorAll(".money");
        let addDom = document.querySelectorAll(".add");
        let reduceDom = document.querySelectorAll(".reduce");
        addDom.forEach(function (item) {
            item.addEventListener('click', function () {
                this.previousElementSibling.innerHTML = Number(this.previousElementSibling.innerHTML) + 1;
                this.parentElement.nextElementSibling.nextElementSibling.innerHTML = Number(this.parentElement.nextElementSibling.nextElementSibling.innerHTML) + Number(this.parentElement.nextElementSibling.innerHTML);
                getTotal();
            })
        })
        reduceDom.forEach(function (item) {
            item.addEventListener('click', function () {
                if (Number(this.nextElementSibling.innerHTML) == 1) {
                    return;
                }
                this.nextElementSibling.innerHTML = Number(this.nextElementSibling.innerHTML) - 1;
                this.parentElement.nextElementSibling.nextElementSibling.innerHTML = Number(this.parentElement.nextElementSibling.nextElementSibling.innerHTML) - Number(this.parentElement.nextElementSibling.innerHTML);
                getTotal();
            })
        })
        //选中后金额,数量添加到购物车
        let allMoneyDom = document.querySelector(".allMoney")
        let allMoneyDomvalue = allMoneyDom.innerHTML;
        checkDom.forEach(function (item) {
            item.addEventListener('click', getTotal)
        })


        //删除
        let delDom = document.querySelectorAll('.glyphicon')
        delDom.forEach(function(item){
            item.addEventListener('click',function(){
                document.querySelector('tbody').removeChild(item.parentElement.parentElement)
                getTotal()
            })
        })





        function getTotal() {
            let buyDom = document.querySelector(".buy").firstChild;
            let buynum = 0;
            let sum = 0
            document.querySelectorAll('.input:checked').forEach(item => {
                buynum +=Number(item.parentElement.nextElementSibling.nextElementSibling.firstChild.nextElementSibling.innerHTML);
                sum += Number(item.parentElement.nextElementSibling.nextElementSibling.nextElementSibling.nextElementSibling.innerHTML);
            })
            allMoneyDom.innerHTML = "￥" + sum;
            buyDom.innerHTML = buynum;
        }
    </script>
</body>

</html>